package cn.practice.weibopractice.controller;


import cn.practice.weibopractice.common.response.JsonResult;
import cn.practice.weibopractice.common.response.StatusCode;
import cn.practice.weibopractice.pojo.dto.CommentDTO;
import cn.practice.weibopractice.pojo.entity.Comment;
import cn.practice.weibopractice.pojo.mapper.CommentMapper;
import cn.practice.weibopractice.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Date;

@Slf4j
@RestController
@Api(tags = "评论模块")
@RequestMapping("/v1/comment/")
@Validated
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation("发布评论")
    public JsonResult insert(@RequestBody @Validated CommentDTO commentdto, @ApiIgnore HttpSession session){
        log.debug("commentdto:{}",commentdto);

        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentdto,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());

        int insert = commentMapper.insertComment(comment);
        if (insert > 0){
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    // 获取评论列表
    @GetMapping("selectByWeiboId")
    @ApiOperation("获取评论列表")
    public JsonResult selectByWeiboId(@RequestParam
                                @Range(min = 200, max = 11200,message = "微博id在200-11200之间")
                                          long weiboId){
        log.debug("weiboId:{}",weiboId);
        return JsonResult.success(commentMapper.selectByWeiboId(weiboId));
    }

}
